package com.bw.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bw.bean.Teachers;
import com.bw.service.TeachersService;

@Controller
@RequestMapping("login")
public class LoginController {
	
	@Autowired
	private TeachersService teachersServiceImpl;
	@RequestMapping("login")
	public String login(){
		return "login";
	}
	@RequestMapping("logout")
	@ResponseBody
	public boolean logout(HttpServletRequest request){
		request.getSession().removeAttribute("teacher");
		return true;
	}
	@RequestMapping("toLogin")
	public String toLogin(Model model, HttpServletRequest request, Teachers teacher){
		Teachers t = teachersServiceImpl.login(teacher);
		if(t != null){
			request.getSession().setAttribute("teacher", t);
		}else{
			model.addAttribute("error", "用户名密码错误，请重新输入");
			return "login";
		}
		System.err.println("ok");
		return "redirect:/";
	}
}
